package com.zjw.zy.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.view.View;

import androidx.annotation.NonNull;

import java.util.Arrays;
import java.util.Map;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/9 11:45
 */
public final class AnimationUtils {


    @NonNull
    private final ObjectAnimator objectAnimator;

    private AnimationUtils(@NonNull ObjectAnimator objectAnimator) {
        this.objectAnimator = objectAnimator;
    }

    public AnimationUtils start() {
        objectAnimator.start();
        return this;
    }

    public AnimationUtils addListener(@NonNull AnimatorListenerAdapter listener) {
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation, boolean isReverse) {
                listener.onAnimationStart(animation, isReverse);
            }

            @Override
            public void onAnimationEnd(Animator animation, boolean isReverse) {
                listener.onAnimationEnd(animation, isReverse);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                listener.onAnimationStart(animation);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                listener.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                listener.onAnimationCancel(animation);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                listener.onAnimationRepeat(animation);
            }
        });
        return this;
    }

    public AnimationUtils duration(long time) {
        objectAnimator.setDuration(time);
        return this;
    }

    public AnimationUtils autoCancel(boolean cancel) {
        objectAnimator.setAutoCancel(cancel);
        return this;
    }

    public AnimationUtils repeatCount(int count) {
        objectAnimator.setRepeatCount(count);
        return this;
    }

    public AnimationUtils repeatMode(int mode) {
        objectAnimator.setRepeatMode(mode);
        return this;
    }

    public static AnimationUtils objectAnimationOfX(@NonNull View view, float... x) {
        return new AnimationUtils(ObjectAnimator.ofFloat(view, "translationX", x));
    }

    public static AnimationUtils objectAnimationOfY(@NonNull View view, float... y) {
        return new AnimationUtils(ObjectAnimator.ofFloat(view, "translationY", y));
    }

    public static AnimationUtils objectAnimationOfAlpha(@NonNull View view, float... alpha) {
        return new AnimationUtils(ObjectAnimator.ofFloat(view, "alpha", alpha));
    }

    public static AnimationUtils objectAnimationOfScaleY(@NonNull View view, float startY, float... scale) {
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("scaleY", scale);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("pivotY", startY);

        return new AnimationUtils(ObjectAnimator.ofPropertyValuesHolder(view, holder1, holder2));
    }

    public static AnimationUtils objectAnimationOfScaleX(@NonNull View view, float startX, float... scale) {
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("scaleX", scale);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("pivotX", startX);

        return new AnimationUtils(ObjectAnimator.ofPropertyValuesHolder(view, holder1, holder2));
    }

    public static AnimationUtils objectAnimationOfMultiProperty(@NonNull View view, Map<String, Float[]> propertyValue) {
        PropertyValuesHolder[] holders = new PropertyValuesHolder[propertyValue.size()];
        int index = 0;
        for (Map.Entry<String, Float[]> entry : propertyValue.entrySet()) {
            float[] args = new float[entry.getValue().length];
            for (int i = 0; i < args.length; i++) {
                args[i] = entry.getValue()[i];
            }
            PropertyValuesHolder holder = PropertyValuesHolder.ofFloat(entry.getKey(), args);
            holders[index++] = holder;
        }
        return new AnimationUtils(ObjectAnimator.ofPropertyValuesHolder(view, holders));
    }
}
